#include "NotifyWindow.h"
#include "ui_NotifyWindow.h"

#include <QDebug>
#include <QCloseEvent>
#include "QDesktopWidget"

NotifyWindow::NotifyWindow(QWidget *parent)
    : QWidget(parent),
      ui(new Ui::NotifyWindow),
      currentEvent(0)
{
    ui->setupUi(this);
    ui->eventDate->clear();
    ui->eventName->clear();
    ui->eventDefinition->clear();

    QObject::connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(dropEvent()));
    QObject::connect(ui->applyButton, SIGNAL(clicked()), this, SLOT(dropEvent()));
    QObject::connect(ui->prevButton,  SIGNAL(clicked()), this, SLOT(prevEvent()));
    QObject::connect(ui->nextButton,  SIGNAL(clicked()), this, SLOT(nextEvent()));

    setAttribute(Qt::WA_QuitOnClose, false);
}

NotifyWindow::~NotifyWindow()
{
    delete ui;
}

void NotifyWindow::addEvent(const Event &event)
{
    if (m_eventList.indexOf(event) > -1) {
        qDebug() << "event was added before...";
        return;
    }
    m_eventList.append(event);
    currentEvent = &m_eventList.last();
    drawEvent(currentEvent);

    qDebug() << "Event list: " << m_eventList.size();
    qDebug() << "Current event:\n" << *currentEvent;

    if (isHidden()) {
        QDesktopWidget *desktop = QApplication::desktop();
        move(desktop->width(), desktop->height());
        show();
    }
}

void NotifyWindow::dropEvent()
{
    if (!currentEvent) {
        return;
    }
    m_eventList.removeOne(*currentEvent);
    if (m_eventList.empty()) {
        close();
        return;
    }
    currentEvent = &m_eventList.last();
    drawEvent(currentEvent);
}

void NotifyWindow::drawEvent(const Event *event)
{
    if (m_eventList.empty() || !event) {
        return;
    }
    ui->eventDate->setText(event->begin().toString("dd MMMM yyyy"));
    ui->eventName->setText(event->name());
    ui->eventDefinition->setText(event->definition());
    QString title = ui->eventDate->text() + ": " + ui->eventName->text();
    setWindowTitle(title);
    updateControls();
}

void NotifyWindow::updateControls()
{
    if (!currentEvent) {
        ui->prevButton->setDisabled(true);
        ui->nextButton->setDisabled(true);
        return;
    }

    if (m_eventList.size() < 2) {
        ui->prevButton->setDisabled(true);
        ui->nextButton->setDisabled(true);
        return;
    }
    ui->prevButton->setEnabled(true);
    ui->nextButton->setEnabled(true);

    int pos = m_eventList.indexOf(*currentEvent);
    if (pos > 0) {
        ui->prevButton->setEnabled(true);
    } else {
        ui->prevButton->setDisabled(true);
    }

    if (pos > -1 && pos < m_eventList.size() - 1) {
        ui->nextButton->setEnabled(true);
    } else {
        ui->nextButton->setDisabled(true);
    }
}

void NotifyWindow::prevEvent()
{
    int pos = m_eventList.indexOf(*currentEvent);
    if (pos > 0) {
        currentEvent = &m_eventList.at(pos - 1);
        drawEvent(currentEvent);
    }
}

void NotifyWindow::nextEvent()
{
    int pos = m_eventList.indexOf(*currentEvent);
    if (pos > -1 && pos < m_eventList.size() - 1) {
        currentEvent = &m_eventList.at(pos + 1);
        drawEvent(currentEvent);
    }
}

void NotifyWindow::closeEvent(QCloseEvent *event)
{
    dropEvent();
    if (m_eventList.empty()) {
        event->accept();
    } else {
        event->ignore();
    }
}
